package com.example.attachment.service;


import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.attachment.po.Attachment;
import com.example.common.service.ServiceImpl;
import com.github.tobato.fastdfs.domain.fdfs.StorePath;
import com.github.tobato.fastdfs.domain.fdfs.ThumbImageConfig;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.multipart.MultipartFile;

import java.awt.image.BufferedImage;
import java.io.FileInputStream;
import java.io.IOException;

@Service
public class AttachmentServiceImpl extends ServiceImpl<Attachment> implements AttachmentService {

    @Autowired
    private FastFileStorageClient storageClient;

    @Autowired
    private ThumbImageConfig thumbImageConfig;

    @Override
    public String upload(MultipartFile file) throws Exception {
        String originalFilename = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf(".") + 1);
        BufferedImage image = null;
        StorePath storePath = null;
        try {
            FileInputStream in = (FileInputStream) file.getInputStream();
            image = javax.imageio.ImageIO.read(in);
            if (image == null || image.getWidth() <= 0 || image.getHeight() <= 0) {
                storePath = this.storageClient.uploadFile(file.getInputStream(), file.getSize(), originalFilename, null);
            } else {
                storePath = this.storageClient.uploadImageAndCrtThumbImage(file.getInputStream(), file.getSize(), originalFilename, null);
            }
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        return storePath.getFullPath();
    }

    @Override
    public BaseMapper<Attachment> getMapper() {
        return null;
    }
}